package com.j2eefast.produce.pms.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsEntrustEntity;
import com.j2eefast.produce.pms.entity.PmsSampleEntryEntity;
import com.j2eefast.produce.pms.service.PmsEntrustService;
import com.j2eefast.produce.pms.service.PmsRegisterEntrustService;
import com.j2eefast.produce.pms.service.PmsSampleEntryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>样品检测数据校验</p>
 *
 * @author: summer
 * @date: 2020-04-27 22:03
 * @web: http://www.j2eefast.com
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/check")
public class PmsSampleCheckController extends BaseController {
	private String urlPrefix = "modules/pms/check";
	@Autowired
	private PmsSampleEntryService pmsSampleEntryService;
//	@Autowired
//	private PmsRegisterEntrustService pmsRegisterEntrustService;

//	@Autowired
//	private PmsEntrustService pmsEntrustService;

	@RequiresPermissions("pms:check:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	@RequestMapping("/list")
	@RequiresPermissions("pms:check:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = pmsSampleEntryService.findPage(params);
		return success(page);
	}

	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("pms:check:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return pmsSampleEntryService.deleteByIds(ids)?success(): error("删除失败!");
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("pms:check:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		PmsSampleEntryEntity pmsEntrustEntity = pmsSampleEntryService.getById(id);
		mmap.put("sampleEntry", pmsEntrustEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:check:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(PmsSampleEntryEntity pmsEntrustEntity) {
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsSampleEntryService.updateById(pmsEntrustEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:check:add")
	@GetMapping("/add/{id}")
	public String add(@PathVariable("id") Long id, ModelMap mmap) {
		return urlPrefix + "/add";
	}



	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:check:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated PmsSampleEntryEntity pmsEntrustEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		String userName=UserUtils.getUserInfo().getName();
		pmsEntrustEntity.setCreateBy(userName);
		return pmsSampleEntryService.save(pmsEntrustEntity) ? success() : error("新增失败!");
	}


	/**
	 * 修改出入库保存
	 */

	@RequiresPermissions("pms:check:checkAdd")
	@BussinessLog(title = "校核", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/checkAdd", method = RequestMethod.POST)
	@ResponseBody
//	@GetMapping("/checkout/{id}")
	public ResponseData checkAdd(@RequestParam(value="id", required=true)Long id,
								 @RequestParam(value="checkStatus", required=true)int checkStatus,
								 ModelMap mmap) {
		System.out.println("1111111111111111111111");
//		ValidatorUtil.validateEntity(pmsSampleEntryEntity);
//		Long ids=Long.parseLong(pmsSampleEntryEntity.g);
		return pmsSampleEntryService.updateCheck(id,checkStatus) ? success() : error("修改失败!");
	}

	/**
	 * 数据录入
	 */
	@GetMapping("/dataEntry/{id}")
	public String task(@PathVariable("id") Long id, ModelMap mmap){
		//mmap.put("comp",  busClassifyService.findCompById(id));
		mmap.put("dataEntry",  id);
		return urlPrefix + "/dataEntry";
	}

	/**
	 * 数据录入保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entry:dataEntry")
	@BussinessLog(title = "数据录入保存", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/dataEntry", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData taskAdd(@Validated PmsSampleEntryEntity pmsEntrustEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		String  createBy= UserUtils.getUserInfo().getName();
		Date data=new Date();
		pmsEntrustEntity.setUpdateBy(createBy);
		pmsEntrustEntity.setUpdateTime(data);
		return pmsSampleEntryService.updateById(pmsEntrustEntity) ? success() : error("新增失败!");
	}





}
